package com.soma.game.controller;

import com.google.common.collect.Maps;
import com.soma.game.entity.Game;
import com.soma.game.entity.GameScore;
import com.soma.game.model.Constants;
import com.soma.game.model.ResEnv;
import com.soma.game.service.GameScoreService;
import com.soma.game.service.GameService;
import com.soma.game.utils.AESUtils;
import com.soma.game.utils.Md5Algorithm;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @program: soma-game
 * @description: 游戏
 * @author: hduprince
 * @create: 2018-07-17 16:43
 **/

@RestController
@RequestMapping("/game")
public class GameController {

    Logger logger = LoggerFactory.getLogger(GameController.class);

    @Resource
    private GameService gameService;

    @Resource
    private GameScoreService gameScoreService;

    @RequestMapping("list")
    public ResEnv list(@RequestParam(value = "language", required = false, defaultValue = "english") String language){
        try {
            List<Game> games = gameService.getOnlineGames(language);
            return ResEnv.success(games);
        } catch (Exception e){
            logger.error("操作失败， {}", ExceptionUtils.getStackTrace(e));
            return ResEnv.fail("操作失败, " + e.getMessage());
        }
    }

    @RequestMapping("")
    public ResEnv detail(@RequestParam("id") Long gameId, @RequestParam(value = "language", required = false, defaultValue = "english") String language){
        try {
            Game game = gameService.load(gameId, language);
            return ResEnv.success(game);
        } catch (Exception e){
            logger.error("操作失败， {}", ExceptionUtils.getStackTrace(e));
            return ResEnv.fail("操作失败, " + e.getMessage());
        }
    }

    @RequestMapping("result")
    public ResEnv saveResult(@RequestParam("userId") Long uid,
                             @RequestParam("gameId") Long gameId,
                             @RequestParam("gameResult") Long score,
                             @RequestParam("timestamp") Long timestamp,
                             @RequestParam("verify") String verify
                             ){
        try {

            String prama = uid+"-"+gameId+"-"+score+"-"+timestamp+"-"+ Constants.md5_key;

            logger.info("请求参数 :" + prama);
            logger.info("verify" + verify);
            String localVerify = Md5Algorithm.getInstance().md5Digest(prama.getBytes("iso8859-1"));
            logger.info("localVerify:" + localVerify);
            //校验加密
            if(!StringUtils.equalsIgnoreCase(localVerify, verify)){
                return ResEnv.fail("校验失败");
            }
            //保存比赛结果
            Map<String, Boolean> result = gameScoreService.saveResult(uid, gameId, score);
            if(score == 0){
                result = Maps.newHashMap();
            }
            return ResEnv.success(result);
        } catch (Exception e){
            logger.error("操作失败， {}", ExceptionUtils.getStackTrace(e));
            return ResEnv.fail("操作失败, " + e.getMessage());
        }
    }

}
